<template>
  <div>
    <!-- class="main-el-contarner" -->
    <!-- 告警设置窗口 -->
    <el-container>
      <el-main>
        <div class="kanbao-bigbox">
          <div class="righttop-search-box2">
            <el-form
              :inline="true"
              :model="param"
              label-width="100px"
              @submit.native.prevent
            >
              <el-form-item label="告警等级：" label-width="90px">
                <el-select
                  v-model="param.warnStatus"
                  placeholder="告警等级"
                  :clearable="true"
                  size="small"
                  style="width: 150px"
                >
                  <el-option label="一般" value="1"></el-option>
                  <el-option label="较严重" value="2"></el-option>
                  <el-option label="严重" value="3"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="" label-width="10px">
                <el-input
                  placeholder="请输入事件名称或关键字检索"
                  clearable
                  v-model="param.name"
                  size="small"
                  style="width: 350px"
                  @keyup.enter.native="onSearch"
                >
                  <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="small" @click="onSearch"
                  >查找</el-button
                >
              </el-form-item>
              <el-form-item style="float: right">
                <!-- <el-button type="text" style="color:#18BC9C;" @click="handleAdd"><i class="el-icon-plus"></i> 新增分类</el-button> -->
              </el-form-item>
            </el-form>
          </div>
          <div class="container-box2">
            <el-table
              :data="tabledatas"
              border
              stripe
              ref="talbes"
              height="29vh"
              :header-cell-style="{
                background: '#cfcfcf',
                color: '#000000',
                'text-align': 'center',
              }"
              :cell-style="{ 'text-align': 'center' }"
              style="width: 100%"
              size="small"
              @selection-change="selectionChange"
              @row-click="handleRowClick"
            >
              <el-table-column
                type="selection"
                header-align="center"
                width="60"
              ></el-table-column>
              <el-table-column prop="event_name" label="告警名称" width="150">
              </el-table-column>
              <el-table-column
                prop="is_turn_to_order"
                label="是否转工单"
                width="150"
              >
                <template slot-scope="scope">
                  <font v-if="scope.row.is_turn_to_order == '0'">是</font>
                  <font v-else-if="scope.row.is_turn_to_order == '1'">否</font>
                  <font v-else>/</font>
                </template>
              </el-table-column>
              <el-table-column prop="alarm_level" label="告警等级" width="120">
                <template slot-scope="scope">
                  <font v-if="scope.row.alarm_level == '1'">一般</font>
                  <font v-else-if="scope.row.alarm_level == '2'">较严重</font>
                  <font v-else-if="scope.row.alarm_level == '3'">严重</font>
                  <font v-else>/</font>
                </template>
              </el-table-column>
              <el-table-column prop="alarm_type" label="告警分类" width="150">
                <template #default="scope">
                  <span v-if="scope.row.alarm_type == 1">故障告警</span>
                  <span v-if="scope.row.alarm_type == 2">离线告警</span>
                  <span v-if="scope.row.alarm_type == 3">消防告警</span>
                  <span v-if="scope.row.alarm_type == 4">周界告警</span>
                  <span v-if="scope.row.alarm_type == 5">周位告警</span>
                </template>
              </el-table-column>

              <el-table-column prop="inform_type" label="通知类型" width="120">
              </el-table-column>
              <el-table-column
                prop="start_date"
                label="开始时段"
                width="120"
                :formatter="dateFormat"
              >
              </el-table-column>
              <el-table-column
                prop="end_date"
                label="结束时段"
                width="120"
                :formatter="dateFormat"
              >
              </el-table-column>
              <el-table-column prop="inform_template" label="通知模板">
              </el-table-column>
              <el-table-column prop="inform_content" label="通知内容">
              </el-table-column>
            </el-table>

            <div class="pagebox">
              <el-pagination
                @size-change="sizeChange"
                @current-change="currentChange"
                :current-page="param.pageNo"
                :page-sizes="[10, 30, 50, 100, 500]"
                :page-size="param.limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="dataTotal"
              >
              </el-pagination>
            </div>
          </div>
          <div style="float: right">
            <el-button type="primary" style="color: #ffffff" @click="bind">
              确定</el-button
            >
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import SideHeader from '@/views/inc/SideHeader'
import Navlanmu from '@/views/inc/Navlanmu'
import LeftMenuMessage from '@/views/inc/LeftMenuMessage'
import NoticeTemplate from '@/views/message/components/NoticeTemplate'
import { getDateTimes, numFormat, getDateWeek } from '@/utils/index.js'
import { getlist, getgridsave, getdeletes } from '@/api/message/list'
import moment from 'moment'

export default {
  name: 'AlarmSetting',
  components: { SideHeader, Navlanmu, LeftMenuMessage, NoticeTemplate },
  data() {
    return {
      loadingdata1: false,
      isloading: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formData: {
        funId: 'msg_alarm_setting',
        datas: [
          {
            event_name: '',
            is_turn_to_order: '',
            alarm_level: '',
            alarm_type: '',
            using_time_range: '',
            inform_type: '',
            inform_template_id: '',
            inform_template: '',
          },
        ],
      },
      param: {
        funId: 'msg_alarm_setting',
        baseSQL: '',
        warnStatus: '',
        name: '',
        offset: 0,
        pageNo: 1,
        limit: 10,
      },
      dataTotal: 0,
      tabledatas: [],
      dialogtitle: '',
      dialogVisible1: false,
      timer: '',
      selectinform_type: '',
      alarmSettingName: '',
    }
  },
  mounted() {
    //
    this.getDatas()

    this.getTempdata()
  },
  created() {
    this.isloading = false
  },
  methods: {
    templateName(e) {
      let tempname = e
      this.formData.datas[0].inform_template = tempname[0].template_name
      this.formData.datas[0].inform_type = tempname[0].inform_type
      this.formData.datas[0].inform_template_id = tempname[0].template_id
      this.dialogVisible1 = false
      // console.log('this.formData.datas[0].inform_template',this.formData.datas)
    },
    // 数据搜索按钮
    onSearch() {
      this.param.pageNo = 1
      this.getDatas()
    },

    // 复选框按钮
    selectionChange(e) {
      // console.log('rree', e)
      if (e.length != '1') {
        this.$message({
          message: '请选择一条数据',
          type: 'error',
        })
        this.toggleSelection(e)
      } else {
        this.alarmSettingName = e
      }
    },
    handleRowClick(e) {
      this.$refs.talbes.toggleRowSelection(e)
      const _selectData = this.$refs.talbes.selection
      this.selectionChange(_selectData)
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.talbes.toggleRowSelection(row)
        })
        this.alarmSettingName = ''
      } else {
        this.$refs.talbes.clearSelection()
      }
    },
    bind() {
      if (this.alarmSettingName.length == '0') {
        this.$message({
          message: '请选择一条数据',
          type: 'error',
        })
      } else {
        this.$emit('getName', this.alarmSettingName)
        // this.toggleSelection(this.alarmSettingName)
      }
    },
    indexMethod(index) {
      return (this.param.pageNo - 1) * this.param.limit + index + 1
    },
    sizeChange(val) {
      this.param.limit = val
      this.getDatas()
    },
    currentChange(val) {
      this.param.pageNo = val
      this.param.offset = (val - 1) * this.param.limit
      this.getDatas()
    },
    //获取数据
    getDatas() {
      if (this.param.warnStatus == '' && this.param.name == '') {
        this.param.baseSQL = ''
      } else {
        this.param.baseSQL =
          'alarm_level like "%' +
          this.param.warnStatus +
          '%" and ' +
          'event_name like "%' +
          this.param.name +
          '%"'
      }
      getlist(this.param).then((res) => {
        if (res.status == '200') {
          let resdata = res.data
          this.tabledatas = resdata.rows
          this.dataTotal = resdata.total
        }
      })
    },
    loadMap() {
      this.timer = new Date().getTime()
      this.dialogVisible1 = true
    },
    getTempdata() {
      let fromdata = {}
      fromdata.funId = 'msg_inform_template'
      getlist(fromdata).then((res) => {
        if (res.status == '200') {
          let resdata = res.data.rows
          // console.log('resdata',resdata)
          let inform_type = []
          resdata.forEach((item) => {
            if (item.auditing == 0) {
              inform_type.push({
                value: item.inform_type,
                label: item.inform_type,
              })
            }
          })
          this.selectinform_type = inform_type
        }
      })
    },
    dateFormat(row, column, cellValue, index) {
      var date = row[column.property]
      if (date == undefined) {
        return ''
      }
      return moment(date).format('HH:mm:ss')
    },
  },
}
</script>
<style>
.kanbao-bigbox .el-loading-spinner .el-loading-text {
  color: #666 !important;
}
</style>
<style scoped>
.el-main {
  padding: 0px !important;
}

.kanbao-bigbox {
  margin-top: 0px;
}
.righttop-search-box2 {
  background: #f8f8f8;
  padding: 0px 10px 5px 10px;
  border-bottom: 1px solid #ececec;
  /* position: fixed; */
}

.righttop-search-box2 .el-form-item {
  margin-bottom: 0px !important;
}

.container-box2 {
  padding: 10px;
}

.pagebox {
  text-align: right;
  padding: 10px;
}
</style>
